// pages/member/follow/component/message.js

import {
  list,
  markAsRead,
  unReadCount,
  conversation
} from "../../../../api/msgPush"
import util from '../../../../utils/util'
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  pageLifetimes:{
    show(){
      this.loadMessage()
      this.loadUnReadCount()
    },
  },
    lifetimes:{
      attached(){
        
      }
    },
  /**
   * 组件的初始数据
   */
  data: {
    messageOrder:{},
    orderCount: ''
  },
 
  

  /**
   * 组件的方法列表
   */
  methods: {
 //每种消息的最后一条
loadMessage(){
    conversation({
      mchId: app.globalData.mchId,
    }).then(res=>{
      res.data.forEach(item=>{
        item.sendDate=util.datetimeFormat(item.sendDate)
        item.msgContent=JSON.parse(item.msgContent)
        if(item.msgKey=='msg-order'){
          this.data.messageOrder=item
        }
      })
      console.log(res.data)
        this.setData({
          messageOrder:this.data.messageOrder
        })
    })
  },

  //未读消息书
  loadUnReadCount() {
    unReadCount().then(res => {
      this.data.unReads = res.data
      if(res.data.length>0){
        this.data.unReads.forEach(item => {
          switch (item.msg_key) {
            case 'msg-order':
              this.data.orderCount = item.msg_num
              break;
          }
        })
      }else{
        this.data.orderCount=''
      }
      this.setData({
        orderCount:this.data.orderCount
      })
    })
  },

    toMessage(){
      wx.navigateTo({
        url: '/pages/member/follow/message/details',
      })
    },
  }
})
